﻿// <copyright file="IdentityCheck.cs" company="">
// Copyright (c) 2013 All Rights Reserved
// </copyright>
// <author></author>
// <date>1/24/2013 6:43:10 AM</date>
// <summary>Implements the IdentityCheck Workflow Activity.</summary>
namespace Xrm.Solutions.FrameworkSample.Workflow
{
    using System;
    using System.Activities;
    using System.ServiceModel;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Workflow;
    using Microsoft.Xrm.Sdk.Query;
    using Xrm.Solutions.FrameworkSample.Common.Entities;
    using Xrm.Solutions.FrameworkSample.Common;

    public class IdentityCheck : BaseCodeActivity
    {
        #region Input Parameters

        [Input("First Name")]
        [RequiredArgument()]
        public InArgument<string> FirstName { get; set; }

        [Input("Last Name")]
        [RequiredArgument()]
        public InArgument<string> LastName { get; set; }

        [Input("Address")]
        [RequiredArgument()]
        public InArgument<string> Address { get; set; }

        [Input("Date of Birth")]
        [RequiredArgument()]
        public InArgument<DateTime> DateOfBirth { get; set; }

        #endregion

        #region Output Parameters

        [Output("Result")]
        public OutArgument<string> Result { get; set; }

        #endregion

        /// <summary>
        /// Executes the workflow activity.
        /// </summary>
        /// <param name="executionContext">The execution context.</param>
        protected override void ExecuteActivity(CodeActivityContext executionContext)
        {
            FailOnException.Set(executionContext, true);
            
            IIdentityCheckService creditCheckService = new IdentityCheckService();
            int result = creditCheckService.RunCheck(FirstName.Get(executionContext),
                LastName.Get(executionContext),
                DateOfBirth.Get(executionContext),
                Address.Get(executionContext));

            if (0 <= result && result <= 40)
                Result.Set(executionContext, "Fail");
            else if (41 <= result && result <= 60)
                Result.Set(executionContext, "Refer");
            else if (61 <= result && result <= 100)
                Result.Set(executionContext, "Pass");
            else
                throw new InvalidPluginExecutionException("Invalid Score");
        }
    }
}